 var utilwork=0;
/*function imagelistToObject(mulitImg,mulitImgid,fun){
	let promiseAll = [], img = [], imgTotal = mulitImg.length;
 for(let i = 0 ; i < imgTotal ; i++){
     promiseAll[i] = new Promise((resolve, reject)=>{
         img[i] = new Image()
	     img[i].crossOrigin = "anonymous";
	     img[i].src = mulitImg[i]	     
	     img[i].id = mulitImgid[i]
         img[i].onload = function(){
              //第i张加载完成
              resolve(img[i])
              UtilProperty[workid].imageObj[img[i].id]=img[i];
         }
     })
 }
 Promise.all(promiseAll).then((img)=>{
     //全部加载完成
    fun();
 })
}*/

function imagelistToObject(mulitImg,workid,fun) {
    var promiseAll = [],
        img = [],
        imgTotal = mulitImg.length;

    var _loop = function _loop(i) {
        promiseAll[i] = new Promise(function (resolve, reject) {
            img[i] = new Image();
            img[i].crossOrigin = "anonymous";
            img[i].src = mulitImg[i];
            img[i].id = mulitImg[i];
            img[i].onload = function () {
                //第i张加载完成
                resolve(img[i]);
                if( UtilProperty.background.backgroundimage.hasOwnProperty(img[i].id)){
                	if( UtilProperty.background.backgroundimage[img[i].id]==null){
                		 UtilProperty.background.backgroundimage[img[i].id] = img[i];
                	}
                }else{
                	UtilProperty[workid].imageObj[img[i].id] = img[i];
                }
                
            };
        });
    };

    for (var i = 0; i < imgTotal; i++) {
        _loop(i);
    }
    Promise.all(promiseAll).then(function (img) {
        //全部加载完成
        fun();
    });
}


//判断是否所有作品的封面的图片都加载完毕
function startcover(){
	idarr.splice(0,1);
 	utilwork++;
	if(utilwork==$(".album .albumimg").length){
	    getCoverCanvas();
	    $("#loadtoast").hide();
		$(".myphotoalbum").show();
	}else{
		//console.log(utilwork);
		//console.log(idarr.length);
		if(idarr.length>0){
			getreadyworker();
		}
	}
 	
 }
 //得到每个作品的封面
 function getCoverCanvas(){
 	for(var a in UtilProperty){
 		if(!(a=='layout_category'||a=='background'||a=='weathericon')){
	 		var tempList=UtilProperty[a];
	 		workid=a;
	 		product_id=tempList.workerInfo.product_id;
	 		if(tempList.worker instanceof Array&&tempList.worker.length>0){
	 			var item=tempList.worker[0]; 			
				//if(item.title=="封面"){
				var canvas=document.createElement("canvas");						
				var context=canvas.getContext("2d");
				var w=boxwindthheight.width;
				var h=boxwindthheight.height;
				var scale;		
				if(item.width / item.height > w / h) {
					scale=w/item.width;
				} else {
					scale=h/item.height;
				}
				canvas.height=item.height*scale;
				canvas.width=item.width*scale;		
				drawToCanvas(canvas,item,context,scale,false);		
				$('.albumitem[product_id="'+product_id+'"][work_id="'+workid+'"] .albumimg').html('<img src="'+canvas.toDataURL()+'"/>');
			}
 		}
 	}
 	
 }






//把当前的图片对象放置到相应的图片控件当中

function placeImageObj(){
	var mulitImg=new Array;				
	var worker=UtilProperty[workid].worker;	
	for(i=0; i<worker.length;i++){		
		var item=worker[i];		
		for(k=0; k<item.control_list.length;k++){				
			UtilProperty[workid].worker[i].control_list[k].ischange=false;											
			if(item.background_id!=0){// lym 2018.01.02 修改 图片加载 原先的的id标志转为url标志	
				if(item.background.image_url.split('thumbnail/')[1]==''){
					var bgurl=item.background.image_url+bgsize_str;
					UtilProperty[workid].worker[i].background.image_url=bgurl;
					if(UtilProperty.background.backgroundimage[bgurl]==undefined){
						mulitImg.push(bgurl);
						UtilProperty.background.backgroundimage[bgurl]=null;
					}	
				}else{					
					if(UtilProperty.background.backgroundimage[item.background.image_url]==undefined){
						mulitImg.push(item.background.image_url);
						UtilProperty.background.backgroundimage[item.background.image_url]=null;
					}	
				}
				
			}
			
			
			var element=item.control_list[k];			
			if(element.control_id==1){				
				if(element.element_list instanceof Array&&element.element_list.length>0){	
					var list=element.element_list[0];
					if(list.image_url!=""){
						if(list.image_url.split('thumbnail/')[1]==''){
							var img_url=list.image_url+imgsize_str;
							UtilProperty[workid].worker[i].control_list[k].element_list[0].image_url=img_url;
							if(!UtilProperty[workid].imageObj[img_url]){												
								if($.inArray(img_url, mulitImg)==-1){
									mulitImg.push(img_url);										
									isfla=true;
								}
							}
							
						}else{
							
							if(!UtilProperty[workid].imageObj.hasOwnProperty(list.image_url)){								
								if($.inArray(list.image_url, mulitImg)==-1){
									mulitImg.push(list.image_url);										
									isfla=true;
								}
							}	
							
						}
					}
				}
			}else if(element.control_id==2){	
				if(element.element_list instanceof Array&&element.element_list.length>0){	
					UtilProperty[workid].worker[i].control_list[k].element_list[0].content=$.trim(element.element_list[0].content);
				}
			}
		
			
		}
	
	}
	
	imagelistToObject(mulitImg,workid,function(){
		//打开编辑作品页面并且合成相应的页面
		//startgetcanvas();
		//遍历图片
		image_list();
		
	})
	
}
//打开已经生成的作品
function openwork(){
	//生成作品
	var ele=$(".album .onfoucs").closest(".albumlist");
	if(ele.attr("status") == 0) {		
		$(".myphotoalbum").hide();
		document.title = "编辑作品";
		$(".editorcon,.editor_view").show();
		creatPage();
		//changeoriginalcanvas(0);	
		changeoriginalcanvas(0,function(data){
 			drawToCanvas(data.canvas,data.item,data.context,data.scale,data.flag);
 		});		
		
	} else if(ele.attr("status") == 1 || ele.attr("status") == 2) {
		// 已提交的作品
		submitworkview();
		$(".myphotoalbum").hide();
		document.title='预览作品';
		$(".gotoedit").hide();
		$(".previewcon").show();
		
		//history.pushState({page: 'previewcon'}, '预览', '');
	}
	$("#loadtoast").hide();
	opentype=0;
}
//打开新建的作品
function openewwork(){
	creatPage();
	changeoriginalcanvas(0);
	$(".uploadcon").hide();
	$(".editorcon,.editor_view").show();
	document.title = "编辑作品";
	$("#loadtoast").hide();
	opentype=0;
	
}


//遍历图片
function image_list(){
		var	image_list=UtilProperty[workid].image_list;
		$("#singerFiles").empty();
		var mulitImg=new Array;
		for(var i=0;i<image_list.length;i++){
			var item=image_list[i];
			var li_detial='';
			if(item.quality!=0){//照片的一边小于900    照片像素过低可能导致，图片模糊   0=正常；1=比例不符合；2=像素不符合
				li_detial='<div class="upload_i_worn"><i class="weui-icon-warn"></i> </div>';
			}
			li_detial=li_detial+'<div class="delimportimg"><i class="fa fa-close fa-fw"></i></div>';
			//var li='<li class="weui-uploader__file" name="'+item.image_id+'"><div class="upload_img"><img src="'+item.image_url+'79x79" image_id="'+item.image_id+'"></div>'+li_detial+'</li>';
			var cur_url=item.image_url+imgsize_str;
			var flag=false;
			for(var k in UtilProperty[workid].imageObj){
				
				if(k.indexOf(item.image_url.split('com/')[1]) !== -1){
					cur_url=k;
					UtilProperty[workid].image_list[i].image_url=cur_url;
					flag=true;
					break;
				}
			}
			
			var bg='background-image:url('+cur_url+')';
			var li='<li class="weui-uploader__file" name="'+item.image_id+'" imgw="'+item.width+'" imgh="'+item.height+'" quality="'+item.quality+'"><div class="upload_img" style="'+bg+'" image_id="'+item.image_id+'" src="'+cur_url+'" ></div>'+li_detial+'</li>';
			$("#singerFiles").append(li);	
			if(!flag){
				mulitImg.push(cur_url);				
			}
					
			
			
		}
		//把图片转成对象
		//imagelistToObject(mulitImg,mulitImgid)
		imagelistToObject(mulitImg,workid,function(){
			//placeImageObj();
			//打开编辑作品页面并且合成相应的页面
			startgetcanvas();
		})
	
}

//打开编辑作品页面并且合成相应的页面
function startgetcanvas(){
	//打开已经生成的作品
	/*for(var i in UtilProperty[workid].imageObj){
		var imgsrc=UtilProperty[workid].imageObj[i].src;	
		var bg='background-image:url('+imgsrc+')';
		$('#singerFiles li[name="'+i+'"] .upload_img').attr({"style":bg,"image_id":i,"src":imgsrc});
		console.log(bg)						
	}*/
	if(opentype==1){
		openwork();
	}else if(opentype==2){
		openewwork()
	}
	
}
